package com.huaxin.hxmodulesystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huaxin.hxmodulesystem.dto.ResponseResult;
import com.huaxin.hxmodulesystem.dto.request.AddOperationLogReqDTO;
import com.huaxin.hxmodulesystem.models.OperationLog;
import com.huaxin.hxmodulesystem.service.OperationLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "日志")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/sys/operationLog")
public class OperationLogController {
    private final OperationLogService operationLogService;

    /**
     * 新增操作日志
     *
     * @param operationLog
     * @return
     */
    @Operation(summary = "新增操作日志", description = "新增操作日志")
    @PostMapping("/addOperationLog")
    public ResponseResult<Boolean> addOperationLog(@RequestBody AddOperationLogReqDTO operationLog) {
        return ResponseResult.ok(operationLogService.addOperationLog(operationLog));
    }

    /**
     * 获取操作日志列表
     *
     * @param pageNum
     * @param pageSize
     * @param userId
     * @param operationType
     * @return
     */
    @Operation(summary = "获取操作日志列表", description = "获取操作日志列表")
    @GetMapping("/getOperationLogList")
    public ResponseResult<Page<OperationLog>> getOperationLogList(@Parameter(description = "页码", name = "pageNum", required = true) @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                                                  @Parameter(description = "每页数量", name = "pageSize", required = true) @RequestParam(name = "pageSize", required = true) Integer pageSize,
                                                                  @Parameter(description = "用户ID", name = "userId") @RequestParam(name = "userId", required = false) String userId,
                                                                  @Parameter(description = "操作类型", name = "operationType") @RequestParam(name = "operationType", required = false) Integer operationType,
                                                                  @Parameter(description = "开始时间", name = "startTime") @RequestParam(name = "startTime") String startTime,
                                                                  @Parameter(description = "结束时间", name = "endTime") @RequestParam(name = "endTime") String endTime) {
        return ResponseResult.ok(operationLogService.getOperationLogList(pageNum, pageSize, userId, operationType, startTime, endTime));
    }

    /**
     * 获取操作日志详情
     *
     * @param id
     * @return
     */
    @Operation(summary = "获取操作日志详情", description = "获取操作日志详情")
    @GetMapping("/getOperationLogDetail")
    public ResponseResult<OperationLog> getOperationLogDetail(@Parameter(description = "操作日志id", name = "id", required = true) @RequestParam(name = "id", required = true) String id) {
        return ResponseResult.ok(operationLogService.getOperationLogDetail(id));
    }

    /**
     * 删除操作日志
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除操作日志", description = "删除操作日志")
    @GetMapping("/deleteOperationLog")
    public ResponseResult<String> deleteOperationLog(@Parameter(description = "操作日志ID", name = "id", required = true) @RequestParam(name = "id", required = true) String id) {
        return operationLogService.deleteOperationLog(id);
    }

    /**
     * 清理操作日志
     */
    @Operation(summary = "清理3个月以前的操作日志", description = "清理3个月以前的操作日志")
    @GetMapping("/clearOperationLogs")
    public ResponseResult<String> clearOperationLogs() {
        return operationLogService.clearOperationLogs();
    }
}
